<template>
  <div>
    <el-card>
      <!--卡片头部-->
      <div slot="header">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="dataForm.key" placeholder="搜索商品" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
          <el-table-column prop="id" header-align="center" align="center" label="商品id">
          </el-table-column>
          <el-table-column prop="name" header-align="center" align="center" label="商品名称">
          </el-table-column>
          <el-table-column prop="price" header-align="center" align="center" label="价格">
          </el-table-column>
          <el-table-column prop="stock" header-align="center" align="center" label="库存">
          </el-table-column>
          <el-table-column prop="payPoint" header-align="center" align="center" label="支付积分">
          </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="createOrderHandle(scope.row.id)">购买</el-button>

            </template>
          </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
                       :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"
                       :total="totalPage"
                       layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 下单 -->
        <create-order v-if="createOrderVisible" ref="CreateOrder" @refreshDataList="getDataList"></create-order>
      </div>
    </el-card>
  </div>
</template>

<script>
import CreateOrder from './pro-create-order'
import request from "@/utils/request";

export default {
  name: "ProductView.vue",
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      createOrderVisible: false
    }
  },
  created() {
    this.getDataList()
  },
  components: {
    CreateOrder
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      request.get('/product/user/list', {
        params: {
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key
        }
      }).then((data) => {
        if (data && data.data.code === 200) {
          this.dataList = data.data.page.list
          this.totalPage = data.data.page.totalCount
        } else {
          this.$message.error(data.data.msg)
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },

    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    createOrderHandle(id) {
      this.createOrderVisible = true
      this.$nextTick(() => {
        this.$refs.CreateOrder.init(id)
      })
    },
  },
}
</script>

<style>
@import "@/css/global.css";
</style>
